import os


def count_words(path, filename):
    """计算一个文件里此语句出现多少次"""
    nums1, nums2, nums3 = 0, 0, 0
    resultList = []
    resultlist1,resultlist2,resultlist3 = [],[],[]
    try:
        with open(path + filename, encoding='gbk') as f_obj:
            lines = f_obj.readlines()
    except FileNotFoundError:
        msg = "Sorry, the file " + filename + " does not exist."
        print(msg)
    else:
        # 计算文件大致出现次数
        for line in lines:
            result1 = line.find("]-[com.cfcc.aiis2.service.routinebiz.vouqueryprint.VouqueryprintService.queryVou")
            if result1 > 1:
                nums1 += 1
                resultlist1.append(line)
            result2 = line.find("]-[com.cfcc.aiis2.service.noteprint.ledgdata.LedgReportService")
            if result2 > 1:
                nums2 += 1
                resultlist2.append(line)
            result3 = line.find("]-[com.cfcc.aiis2.service.noteprint.imredabala.ImRedabalaFibyService")
            if result3 > 1:
                nums3 += 1
                resultlist3.append(line)
    resultList.append("The " + filename + " has queryVou about " + str(nums1) + " number.\n")
    resultList.extend(resultlist1)
    resultList.append("The " + filename + " has LedgReportService about " + str(nums2) + " number.\n")
    resultList.extend(resultlist2)
    resultList.append("The " + filename + " has ImRedabalaFibyService about " + str(nums3) + " number.\n")
    resultList.extend(resultlist3)
    return resultList

def count_words(path):
    params = os.path.split(path)
    count_words(params[0],params[1])

path = "/media/thseus/KINGSTON/FIN-1229/"
doc = "FINN1S3"
files = os.listdir(path + doc + os.sep)
writeList = []
for filename in files:
    resultList = count_words(path + doc + os.sep, filename)
    writeList.extend(resultList)
# print(writeList)
with open(path + os.sep + doc + ".txt", 'w') as f:
    f.writelines(writeList)
